var resetType = false // 重置状态 
if (sessionStorage.getItem('MyCollection_curr') != null) {
    sessionStorage.setItem('MyCollection_curr',1)
}
// 排序
function creatCompare(propertyName) {
    return function (obj1, obj2) {
        var value1 = obj1[propertyName];
        var value2 = obj2[propertyName];
        if (value1 > value2) {
            return -1
        } else if (value1 < value2) {
            return 1
        } else {
            return 0
        }
    }
}
//填充收藏表格
function initTable(data) {
    if (isNull(colType)) {
        colType = "";
    } else {
        colType = $("#collectType").val()
    }
    ipt_collect_date = $('#ipt_date').val();
    ipt_collect_name = $('#ipt_search').val();
    tdata = dealData(data);
    table.render({
        elem: '#collectedTable'
        , data: tdata
      , toolbar: true
      ,page: {
        limit:20,
        curr:sessionStorage.getItem('MyCollection_curr') ? sessionStorage.getItem('MyCollection_curr') : 1
        }
      , toolbar: '#tbCollectlToolBar'   //这里在左边显示，然后指定到模版id
      , cols: [[
          { type: 'numbers', title: '序号', align: 'center', width: 100 }
          , { field: 'NAME', title: '服务名称', align: 'center' }
          , { field: 'TYPE', title: '服务类型', align: 'center' }
          , { field: 'SAVEDATE', title: '收藏时间', align: 'center' }
          , { align: 'center', title: '操作', toolbar: '#collect-table-operate' }
          , { align: 'center', title: '取消收藏', toolbar: '#collect-table-operate2' }
      ]]
      , response: {
          statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
      }
      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
          return {
              "code": res.status, //解析接口状态
              "msg": res.message, //解析提示文本
              "count": res.total, //解析数据长度
              "data": res.rows.item //解析数据列表
          };
      }
      , done: function (res, curr, count) { 
        sessionStorage.setItem('MyCollection_curr',curr)
          $("[data-field = 'TYPE']").children().each(function () {
              for (var key in collectTypes) {
                  var oneData = collectTypes[key];
                  if ($(this).text() == "" + oneData.ZDVALUE + "") {
                      $(this).text(oneData.ZDTITLE);
                  }
              }
          });
      }
    });
    initDropdown();
    $("#collectType").val(colType);
    $('#ipt_date').val(ipt_collect_date);
    $('#ipt_search').val(ipt_collect_name);
    layui.form.render('select');
    //日期时间范围
    laydate.render({
        elem: '#ipt_date'
    , type: 'date'
    , range: true
    });
}
//取消收藏
function cancelCollected(obj){
    var results = null;
    layer.confirm("确认要取消收藏吗？", { title: "取消收藏" }, function (index){
        if (index) {
            if(obj.data.TYPE == "1"){
                results = sendAjax(modelURL + "resource/deleteCollectService", "POST", {
                    id: obj.data.COLLECTSERVICEID
                });
                
            }else{
                // results = sendAjax(modelURL + "apply/getApplysFavorite", "POST", {
                //     id: obj.data.ID,
                //     isFavorite:"1"
                // });
                results = sendAjax(modelURL + "/apply/updateApplysFavorite", "POST", {
                    id: obj.data.NO,
                    isFavorite: "0",
                    style:obj.data.STYLE,
                    userNo: portaluserno
                });
            }
            if (results.code == 200) {
                var TBData = getTbData(); 
                initTable(TBData)
            }else{
                layer.msg("取消收藏失败！",{icon:5});
            }
            layer.close(index);
        }
    })
}
//点击运行按钮
function runningApply(name,NO){
    window.parent.window.parent.window.$(".layui-tab-title .layui-this").removeClass("layui-this");
    window.parent.window.parent.window.$(".layui-tab-title #header_tab2").addClass("layui-this");
    window.parent.window.parent.window.$("#applicationCenter").hide();
    window.parent.window.parent.window.$("#fmap").attr("src","/MapServer/Portal/3DViewer.html?mode=default");
    window.parent.window.parent.window.$("#fmap").show();
    window.parent.window.parent.window.$("#fmap").attr("jumpping2Tools",name);
    window.parent.window.parent.window.$("#fmap").attr("jumpping2ToolsNo",NO);
}
//获取表格数据
function getTbData(){
    var data = null;
    var results = sendAjax(modelURL + "resource/getMyCollections", "POST", {
        serviceType:"all",
        userNo:portaluserno
    });
    if (results.code == 200) {
        data = results.data; 
        data.sort(creatCompare("SAVEDATE"))
    }else{
        layui.layer.msg("获取数据失败！",{icon:5})
    }
    return data;
}
//对获取到的数据进行处理
function dealData(data){
    var strPositionData = getSpecialDataDict("position");
    data.map(function(item, index) {
        if(item.TYPE == "1"){
            item["NAME"] = item.LAYERALIAS;
            for (var key in strPositionData) {
                oneDataDict = strPositionData[key];
                if(oneDataDict.ID == item.POSITION){
                    item.POSITION = oneDataDict.ZDVALUE
                }
            }
        }
        if(item.SAVEDATE != ""){
            item.SAVEDATE = formatDate(item.SAVEDATE)
        }
        return item;
    })
    return data;
}
//查询数据
function searchData(serviceType,startTime,endTime,name){
    if(isNull(serviceType)){
        serviceType = "all"
    }
    var results = sendAjax(modelURL + "resource/getMyCollections", "POST", {
        userNo:portaluserno,
        serviceType:serviceType,
        startTime:startTime,
        endTime:endTime,
        name:name
    });
    if (results.code == 200) { 
        sessionStorage.setItem('MyCollection_curr',1)
        initTable(results.data)
    }else{
        layer.msg("取消收藏失败！",{icon:5});
    }
}
//获取下拉列表
function initDropdown() {
    for (var i = 0; i < collectTypes.length; i++) {
        $("[name='collectType']").append("<option value='" + collectTypes[i].ZDVALUE + "'>" + collectTypes[i].ZDTITLE + "</option>");
    }
    layui.form.render('select');
    layui.form.on('select', function (data) {
        $("#collectType").val(data.value);
        colType = $("#collectType").val()
    });
}